package com.xrkol.sharelib.listener;

/*
 * Copyright 2010. 
 * 
 * This document may not be reproduced, distributed or used 
 * in any manner whatsoever without the expressed written 
 * permission of xrkol Corp. 
 * 
 * $Rev: Rev $
 * $Author: Author $
 * $LastChangedDate: LastChangedDate $
 *
 */

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import com.xrkol.sharelib.constants.BaseConstants;

/**
 * @author peng.xia
 * 
 */
public class ApplicationInitialListener implements ApplicationListener<ContextRefreshedEvent> {

    /**
     * 避免刷新root-application容器和project-name容器时执行两次代码的类变量
     */
    private volatile boolean isStart = false;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (!isStart) {
            isStart = true;
        } else {
            clearTempFile();
        }
    }

    private void clearTempFile() {
        String tempFilePath = System.getProperty("webRoot") + BaseConstants.TEMP_FILE;
        File tempFile = new File(tempFilePath);
        if (tempFile.exists()) {
            try {
                FileUtils.deleteDirectory(tempFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
